﻿Imports System

Public Class ToolKitUri
    Inherits System.Uri
    Public Enum FileExtensionType
        unknown
        asmx
        asp
        aspx
        axd
        gif
        htm
        html
        ico
        icon
        info
        jpg
        js
        php
        png
        svc
        sql
        torrent
        txt
        pdf
        xml
        cs
        vb
    End Enum
    Public Enum DomainExtensionType
        none
        com
        net
        org
        edu
        gov
        int
        mil
        biz
        info
        jobs
        mobi
        name
        tel
        br
        ca
        cn
        cx
        fr
        [in]
        jp
        ru
        us
    End Enum
    Public Structure FileType
        Private _OriginalString As String
        Private _Name As String
        Private _Extension As FileExtensionType
        Public Sub New(ByVal OriginalString As String)
            OriginalString = OriginalString.Replace("/", "")
            If OriginalString.IndexOf(".") <= 0 Then OriginalString = "Default.aspx"
            If OriginalString.IndexOf("?") >= 0 Then
                OriginalString = OriginalString.Substring(0, OriginalString.IndexOf("?"))
            End If
            Dim LastIndexOfDot As Int32 = OriginalString.LastIndexOf(".")
            Dim TempExtension As String = OriginalString.Substring(LastIndexOfDot + 1, OriginalString.Length - (LastIndexOfDot + 1))
            If [Enum].GetNames(GetType(FileExtensionType)).Contains(TempExtension) Then
                _Extension = DirectCast([Enum].Parse(GetType(FileExtensionType), OriginalString.Substring(LastIndexOfDot + 1, OriginalString.Length - (LastIndexOfDot + 1)), True), FileExtensionType)
            Else
                _Extension = FileExtensionType.unknown
            End If
            _OriginalString = OriginalString
            _Name = OriginalString.Substring(0, LastIndexOfDot)
        End Sub
        Public ReadOnly Property OriginalString() As String
            Get
                Return _OriginalString
            End Get
        End Property
        Public ReadOnly Property Name() As String
            Get
                Return _Name
            End Get
        End Property
        Public ReadOnly Property Extension() As FileExtensionType
            Get
                Return _Extension
            End Get
        End Property
        Public ReadOnly Property IsImage() As Boolean
            Get
                Return ToolKitUri.IsImage(Me.Extension)
            End Get
        End Property
    End Structure
    Public Structure DomainType
        Private _OriginalString As String
        Private _Name As String
        Private _Domain As String
        Private _Extension As DomainExtensionType
        Public Sub New(ByVal OriginalString As String)
            OriginalString = OriginalString.Replace("/", "")
            If OriginalString.IndexOf(".") >= 0 Then
                Dim LastIndexOfDot As Int32 = OriginalString.LastIndexOf(".")
                _OriginalString = OriginalString
                _Name = OriginalString.Substring(0, LastIndexOfDot)
                _Extension = DirectCast([Enum].Parse(GetType(DomainExtensionType), OriginalString.Substring(LastIndexOfDot + 1, OriginalString.Length - (LastIndexOfDot + 1)), True), DomainExtensionType)
                If OriginalString.IndexOf(".") > 0 Then
                    Dim ArrHost As String() = OriginalString.Split("."c)
                    _Domain = (ArrHost(ArrHost.Length - 2) + "." + ArrHost(ArrHost.Length - 1)).ToLower()
                Else
                    _Domain = OriginalString.ToLower()
                End If
            Else
                _Name = OriginalString
                _Domain = OriginalString
                _Extension = DomainExtensionType.none
            End If
        End Sub
        Public ReadOnly Property OriginalString() As String
            Get
                Return _OriginalString
            End Get
        End Property
        Public ReadOnly Property Name() As String
            Get
                Return _Name
            End Get
        End Property
        Public ReadOnly Property DomainName() As String
            Get
                Return _Domain
            End Get
        End Property
        Public ReadOnly Property Extension() As DomainExtensionType
            Get
                Return _Extension
            End Get
        End Property
    End Structure

    Private IsDomainLoaded As Boolean = False
    Public ReadOnly Property Domain() As DomainType
        Get
            If Not IsDomainLoaded Then
                _Domain = New DomainType(Me.Host)
                IsDomainLoaded = True
            End If
            Return _Domain
        End Get
    End Property
    Private _Domain As DomainType

    Private IsFileLoaded As Boolean = False
    Public ReadOnly Property File() As FileType
        Get
            If Not IsFileLoaded Then
                Dim TempFileName As String = System.IO.Path.GetFileName(Me.PathAndQuery)
                If TempFileName.IndexOf(".") > 0 Then
                    _File = New FileType(TempFileName)
                    IsFileLoaded = True
                Else
                    For Each f As String In Segments
                        If f.IndexOf(".") > 0 Then
                            _File = New FileType(f)
                            IsFileLoaded = True
                            Exit For
                        End If
                    Next
                End If
                If Not IsFileLoaded Then
                    _File = New FileType("Default.aspx")
                    IsFileLoaded = True
                End If
            End If
            Return _File
        End Get
    End Property
    Private _File As FileType

    Public Sub New(ByVal RawURL As String)
        MyBase.New(ConvertUrl(RawURL))
    End Sub
    Public Sub New(ByVal URL As Uri)
        MyBase.New(URL.OriginalString)
    End Sub
    Public Shared Function ConvertUrl(ByVal RawURL As String) As String
        Dim TempFileName As String = System.IO.Path.GetFileName((New Uri(RawURL)).PathAndQuery)
        If TempFileName.IndexOf(".") > 0 Then
            Dim TempFile As FileType = New FileType(TempFileName)
            If TempFile.Extension = FileExtensionType.unknown Then
                RawURL.Replace(TempFileName, TempFileName + "/Default.aspx")
            End If
        End If
        If RawURL.IndexOf("?404;") > 0 Then
            Dim lenRLP As Int32 = RawURL.IndexOf("?404;") + 5
            RawURL = RawURL.Substring(lenRLP, RawURL.Length - lenRLP)
        End If
        RawURL = RawURL.Replace(":80", "")
        Dim AppPath As String = HttpContext.Current.Request.ApplicationPath
        If AppPath.Length > 1 AndAlso RawURL.Contains(AppPath) Then
            RawURL = RawURL.Substring(0, RawURL.IndexOf(AppPath)) + _
            RawURL.Substring(RawURL.IndexOf(AppPath) + AppPath.Length, RawURL.Length - RawURL.IndexOf(AppPath) - AppPath.Length)
        End If
        Return RawURL
    End Function
    Public Shared Function IsImage(ByVal Ext As FileExtensionType) As Boolean
        Select Case Ext
            Case FileExtensionType.gif
                Return True
            Case FileExtensionType.jpg
                Return True
            Case FileExtensionType.png
                Return True
            Case FileExtensionType.ico
                Return True
            Case FileExtensionType.icon
                Return True
            Case Else
                Return False
        End Select
    End Function
End Class
